/**
 * 研发项目阶段配置
 */

// 阶段定义
const PHASES = {
  // 基础阶段
  'pre_research': { name: '预研', color: '#909399', order: 1 },
  'design': { name: '设计', color: '#409EFF', order: 2 },
  'prototype': { name: '样机', color: '#E6A23C', order: 3 },
  'testing': { name: '测试', color: '#67C23A', order: 4 },
  'mass_production': { name: '量产', color: '#F56C6C', order: 5 },
  'completion': { name: '完成', color: '#67C23A', order: 6 },
  
  // 子阶段
  'design.hw': { name: '硬件设计', color: '#409EFF', order: 2.1, parent: 'design' },
  'design.sw': { name: '软件设计', color: '#409EFF', order: 2.2, parent: 'design' },
  'prototype.hw': { name: '硬件样机', color: '#E6A23C', order: 3.1, parent: 'prototype' },
  'prototype.sw': { name: '软件样机', color: '#E6A23C', order: 3.2, parent: 'prototype' },
  'testing.internal': { name: '内部测试', color: '#67C23A', order: 4.1, parent: 'testing' },
  'testing.customer': { name: '客户测试', color: '#67C23A', order: 4.2, parent: 'testing' }
};

// 阶段文档配置
const PHASE_DOCS = {
  'pre_research': [
    { type: 'market_analysis', name: '市场分析', description: '市场需求和竞争分析', required: true },
    { type: 'feasibility_study', name: '可行性研究', description: '技术可行性分析报告', required: true },
    { type: 'preliminary_spec', name: '初步规格', description: '初步产品规格说明书', required: true }
  ],
  
  'design': [
    { type: 'design_spec', name: '设计规格', description: '详细设计规格说明书', required: true },
    { type: 'system_design', name: '系统设计', description: '系统架构设计文档', required: true },
    { type: 'bom_list', name: 'BOM清单', description: '物料清单', required: true },
    { type: 'schematic', name: '原理图', description: '电路原理图', required: false }
  ],
  
  'design.hw': [
    { type: 'pcb_design', name: 'PCB设计', description: 'PCB设计文件', required: true },
    { type: 'hw_spec', name: '硬件规格', description: '硬件详细规格说明', required: true },
    { type: '3d_model', name: '3D模型', description: '产品3D模型文件', required: false }
  ],
  
  'design.sw': [
    { type: 'sw_design', name: '软件设计', description: '软件设计文档', required: true },
    { type: 'protocol_spec', name: '协议规格', description: '通信协议规格说明', required: true },
    { type: 'sw_framework', name: '软件框架', description: '软件架构框架文档', required: true }
  ],
  
  'prototype': [
    { type: 'prototype_spec', name: '样机规格', description: '样机规格说明书', required: true },
    { type: 'prototype_test', name: '样机测试', description: '样机测试报告', required: true }
  ],
  
  'prototype.hw': [
    { type: 'hw_prototype_doc', name: '硬件样机文档', description: '硬件样机说明文档', required: true },
    { type: 'hw_sample_test', name: '硬件样机测试', description: '硬件样机测试报告', required: true }
  ],
  
  'prototype.sw': [
    { type: 'sw_prototype_doc', name: '软件原型文档', description: '软件原型说明文档', required: true },
    { type: 'sw_function_test', name: '软件功能测试', description: '软件功能测试报告', required: true }
  ],
  
  'testing': [
    { type: 'test_plan', name: '测试计划', description: '测试计划文档', required: true },
    { type: 'test_case', name: '测试用例', description: '测试用例文档', required: true },
    { type: 'test_report', name: '测试报告', description: '测试结果报告', required: true }
  ],
  
  'testing.internal': [
    { type: 'internal_test_report', name: '内部测试报告', description: '内部测试结果报告', required: true },
    { type: 'issue_tracking', name: '问题跟踪', description: '问题跟踪记录', required: true }
  ],
  
  'testing.customer': [
    { type: 'customer_test_report', name: '客户测试报告', description: '客户测试结果报告', required: true },
    { type: 'customer_feedback', name: '客户反馈', description: '客户反馈记录', required: true }
  ],
  
  'mass_production': [
    { type: 'production_spec', name: '生产规格', description: '生产规格说明书', required: true },
    { type: 'production_process', name: '生产工艺', description: '生产工艺流程文档', required: true },
    { type: 'quality_control', name: '质量控制', description: '质量控制计划文档', required: true },
    { type: 'production_test', name: '生产测试', description: '生产测试规范', required: true }
  ],
  
  'completion': [
    { type: 'final_report', name: '项目总结', description: '项目总结报告', required: true },
    { type: 'user_manual', name: '用户手册', description: '用户使用手册', required: true },
    { type: 'maintenance_manual', name: '维护手册', description: '维护手册', required: true },
    { type: 'project_archive', name: '项目归档', description: '项目归档记录', required: true }
  ]
};

// 状态与阶段映射
const STATE_PHASE_MAPPING = {
  'pending': 'pre_research',
  'review': 'pre_research',
  'active.hw_confirm': 'design.hw',
  'active.hw_design': 'design.hw',
  'active.debug': 'design.sw',
  'active.testing': 'testing.internal',
  'active.customer_confirm': 'testing.customer',
  'active.order_preparation': 'mass_production',
  'active.production': 'mass_production',
  'active.feedback': 'completion',
  'completed': 'completion'
};

/**
 * 根据状态获取当前阶段
 * @param {String} state 状态代码
 * @returns {String} 阶段代码
 */
function getCurrentPhase(state) {
  return STATE_PHASE_MAPPING[state] || 'pre_research';
}

/**
 * 获取阶段名称
 * @param {String} phase 阶段代码
 * @returns {String} 阶段名称
 */
function getPhaseName(phase) {
  if (!phase || !PHASES[phase]) return '未知阶段';
  return PHASES[phase].name;
}

/**
 * 获取阶段颜色
 * @param {String} phase 阶段代码
 * @returns {String} 阶段颜色
 */
function getPhaseColor(phase) {
  if (!phase || !PHASES[phase]) return '#909399';
  return PHASES[phase].color;
}

/**
 * 获取阶段所需文档
 * @param {String} phase 阶段代码
 * @returns {Array} 文档列表
 */
function getPhaseRequiredDocs(phase) {
  if (!phase || !PHASE_DOCS[phase]) return [];
  
  // 获取当前阶段文档
  const docs = [...PHASE_DOCS[phase]];
  
  // 如果是子阶段，还需要包含父阶段的文档
  const phaseInfo = PHASES[phase];
  if (phaseInfo && phaseInfo.parent && PHASE_DOCS[phaseInfo.parent]) {
    docs.push(...PHASE_DOCS[phaseInfo.parent]);
  }
  
  return docs;
}

/**
 * 检查阶段文档完成情况
 * @param {Array} projectDocs 项目文档列表
 * @param {String} phase 阶段代码
 * @returns {Object} 文档完成情况
 */
function checkPhaseDocsCompletion(projectDocs, phase) {
  const requiredDocs = getPhaseRequiredDocs(phase);
  
  // 如果没有要求的文档，返回100%完成
  if (requiredDocs.length === 0) {
    return { total: 0, completed: 0, percentage: 100 };
  }
  
  // 统计已完成的必需文档
  let completedRequired = 0;
  let totalRequired = 0;
  
  for (const doc of requiredDocs) {
    if (doc.required) {
      totalRequired++;
      
      // 检查项目文档中是否存在该类型并完成
      const isCompleted = projectDocs.some(
        projectDoc => projectDoc.type === doc.type && projectDoc.status === 'completed'
      );
      
      if (isCompleted) {
        completedRequired++;
      }
    }
  }
  
  // 计算完成百分比
  const percentage = totalRequired > 0 ? Math.round((completedRequired / totalRequired) * 100) : 100;
  
  return {
    total: totalRequired,
    completed: completedRequired,
    percentage
  };
}

/**
 * 获取所有阶段列表
 * @param {Boolean} includeSubPhases 是否包含子阶段
 * @returns {Array} 阶段列表
 */
function getAllPhases(includeSubPhases = true) {
  if (!includeSubPhases) {
    // 只返回主阶段
    return Object.keys(PHASES)
      .filter(key => !key.includes('.'))
      .map(key => ({
        code: key,
        name: PHASES[key].name,
        color: PHASES[key].color,
        order: PHASES[key].order
      }))
      .sort((a, b) => a.order - b.order);
  }
  
  // 返回所有阶段
  return Object.keys(PHASES)
    .map(key => ({
      code: key,
      name: PHASES[key].name,
      color: PHASES[key].color,
      order: PHASES[key].order,
      parent: PHASES[key].parent
    }))
    .sort((a, b) => a.order - b.order);
}

module.exports = {
  PHASES,
  PHASE_DOCS,
  STATE_PHASE_MAPPING,
  getCurrentPhase,
  getPhaseName,
  getPhaseColor,
  getPhaseRequiredDocs,
  checkPhaseDocsCompletion,
  getAllPhases
}; 